package src.model.gameWorld;

import java.util.HashMap;
import java.util.Set;
import java.util.Map;

import java.io.File;

import src.model.gameObject.GameObject;
import src.model.gameObject.GameObjectType;
import src.model.gameObject.GameObjectSubtype;

/**
 * <b>Overview</b><br>
 * <p>a GameDataObject represents all the information about a GameObject that is
 * viewable by the owning player at a given time.  They are used by the view
 * to draw visual representaions of the objects on the screen.</p>
 * <br><br>
 * <b>Responsibilities</b><br>
 * <p>GameDataObjects are responsible for encapsulating the data required by
 * the view to represent an object.</p>
 * <br><br>
 * <b>Collaborators</b><br>
 * <p>None.</p><br>
 * <b>Implementor</b> - Brent <br>
 * <b>Testor</b> - Trstan <br>
 *
 * @author Brent
 *
 */
public class GameDataObject
{
	private HashMap<String, String> data;
	//This is how I'm doing images for now
	String filepath;

	/**
	 * Constructs a GameDataObject with the supplied data.
	 * The mapping that is returned is of Strings to Strings, and is of the
	 * form &lt;value name&gt; -&gt; &lt;value&gt;
	 * <br><br>
	 * example:
	 * 		"Current Health" -&gt; "27"
	 * 		"Maximum Health" -&gt; "30"
	 *
	 * @param data - a java.util.HashMap as described above
	 */
	public GameDataObject(HashMap<String, String> data)
	{
		this.data = data;
	}

	/**
	 * Constructs a new GameDataObject that is identical to the given
	 * GameDataObject, including ID and data.
	 *
	 * @param gdo - GameDataObject to be copied
	 */
	public GameDataObject(GameDataObject gdo)
	{
		this.data = new HashMap<String, String>();

		HashMap<String, String> newData = gdo.getAllInfo();
		Set<Map.Entry<String, String>> newDataSet = newData.entrySet();
		for(Map.Entry<String, String> mapEntry : newDataSet){
			data.put(mapEntry.getKey(), mapEntry.getValue());
		}

		this.filepath = gdo.getFilepath();
	}

	/**
	 * Constructs a new GameDataObject from the given GameObject.
	 *
	 * @param go - GameObject to be represented
	 */
	public GameDataObject(GameObject go)
	{
		//Still have to figure this fucker out
		data = new HashMap<String, String>();

		if(go.getType() == GameObjectType.TERRAIN){
			data.put("Type", "TERRAIN");

			if(go.getSubtype() == GameObjectSubtype.SAND){
				data.put("Subtype", "SAND");
				filepath = "SAND";
			}
			if(go.getSubtype() == GameObjectSubtype.WATER){
				data.put("Subtype", "WATER");
				filepath = "WATER";
			}
		}
		if(go.getType() == GameObjectType.UNIT){
			filepath += "sprites" + File.separator;
			data.put("Type", "UNIT");

			if(go.getSubtype() == GameObjectSubtype.CORSAIR){
				data.put("Subtype", "CORSAIR");
				filepath = "CORSAIR";
			}
		}
	}

	/**
	 * Since the purpose of a GameDataObject is to eventually be displayed,
	 * this method can be used to retreive the filepath to the image used
	 * for display
	 * @return a java.lang.String representing the filepath
	 */
	public String getFilepath(){
		return filepath;
	}

	/**
	 * Get's the GameDataObject's type
	 * @return the GameDataObject's type
	 */
	public GameObjectType getType(){
		if(data.get("Type") == null)
			return null;

		String type = data.get("Type");

		if(type.equals("UNIT"))
			return GameObjectType.UNIT;
		if(type.equals("STRUCTURE"))
			return GameObjectType.STRUCTURE;
		if(type.equals("TERRAIN"))
			return GameObjectType.TERRAIN;
		if(type.equals("ITEM"))
			return GameObjectType.ITEM;
		if(type.equals("AREAEFFECT"))
			return GameObjectType.AREAEFFECT;


		return GameObjectType.UNIT;
	}

	/**
	 * Used to get a specific piece of data from the GameDataObject
	 *
	 * @param key - the key of the value being requested
	 * @return the value being requiested
	 */
	public String getInfo(String key)
	{
		return data.get(key);
	}

	/**
	 * Used to get all the information encapsulated by this GameDataObject.
	 * The mapping that is returned is of Strings to Strings, and is of the
	 * form &lt;value name&gt; -&gt; &lt;value&gt;
	 * <br><br>
	 * example:
	 * 		"Current Health" -&gt; "27"
	 * 		"Maximum Health" -&gt; "30"
	 *
	 * @return java.util.HashMap in the form described
	 */
	public HashMap<String, String> getAllInfo()
	{
		return data;
	}

	/**
	 * Used to replace all the information encapsulated by this GameDataObject.
	 * The mapping that is required as a parameter is of Strings to Strings,
	 * and is of the form &lt;value name&gt; -&gt; &lt;value&gt;
	 * <br><br>
	 * example:
	 * 		"Current Health" -&gt; "27"
	 * 		"Maximum Health" -&gt; "30"
	 *
	 * @param data - java.util.HashMap in the form described above
	 */
	public void replaceAllInfo(HashMap<String, String> data)
	{
		this.data = data;
	}
}
